var set_interval;
var tempo_troca = 6000;
var qtd_int;

$(document).ready(function(){
    
    $('#form_login').submit(function(){    
        $.post(URLBASE + '/login/loginajx', $(this).serialize() , function(data)
        {   
            if(data.ok){
                $('#form_login').html(data.html); 
            }
            else alert(data.msg);
            return false; 
        }, 'json');
        
        return false;
    });
    
    $(document).on("click", ".btn_cal_l,.btn_cal_r",function(){
        $('.calendario').html('<img src="'+URLBASE+'/public/media/images/loader_vm.gif" class="loading" />');
        $('.lista_calendario').html('');
        $.post(URLBASE + '/index/calendario',{ data:$(this).attr('data') }, function(data)
        {   
            $('.calendario').html(data.html1);   
            $('.lista_calendario').html(data.html2);
            $('#script').html(data.script);
        }, 'json'); 
        return false;
    });
    
    var qtd_int = $('.banner').length;
    
    if(qtd_int > 1){
        set_interval = setInterval(function(){ 

            var pos = $('.banner:visible').attr('id');
            pos = pos.replace('banner','');
            var pst = pos;

            pos = parseInt(pos) + 1;
            if(pos >= $('.banner').length) pos = 0;

            setBanners(pos,pst);

        },tempo_troca);
    }
    
    $(document).on("click", ".lnkitg a",function(){
        var pst = $('.banner:visible').attr('id');
        pst = pst.replace('banner','');
        var pos = $(this).attr('itemid');
        setBanners(pos,pst); 
    });
    
    $('.lnkitg a').mouseover(function(){
        $('.imgs img').hide();
        $('#'+$(this).attr('itemref')).fadeIn(500);
    }).mouseout(function(){
        $('.imgs img').hide();
    });
    
});

function setBanners(escolhido,pst){
    clearTimeout(set_interval);
    $('#banner'+pst).fadeOut(300);// hide();
    $('#banner'+escolhido).fadeIn(800);
    $('.lnkitg a').removeClass('action');
    $('#lnkb'+escolhido).addClass('action');

    var qtd_int = $('.banner').length;    

    if(qtd_int > 1){
        set_interval = setInterval(function(){ 

            var pos = $('.banner:visible').attr('id');
            pos = pos.replace('banner','');
            var pst = pos;

            pos = parseInt(pos) + 1;
            if(pos >= $('.banner').length) pos = 0;

            setBanners(pos,pst);

        },tempo_troca);
    }
}
